<?php

namespace Home\Controller;

use Think\Controller;
use Common\Controller\DefaultController;
use Admin\Model\ColumnModel;

class MemberController extends DefaultController {

    //会员注册
    public function memberReg(){

        if(IS_AJAX){

            $member = D('Member');

            $verCode = I('verCode');

            $check = $this->checkVerCode($verCode);

            if($check){

                $numbered = date("Y-m-dH-i-s");
                $numbered = str_replace("-","",$numbered);
                $numbered .= rand(1000,9999);

                $member_renum = I('reNum','');

                $_POST['member_tel'] = I('tel');
                $_POST['member_realname'] = I('realName');
                $_POST['member_nickname'] = I('realName');
                $_POST['member_idcard'] = I('idCard');
                $_POST['member_pwd'] = I('pwd');
                $_POST['member_mynum'] = $numbered;
                $_POST['member_renum'] = $member_renum;
                $_POST['member_state'] = 0;
                $_POST['add_time'] = time();
                $_POST['display']  = 0;
                $_POST['logins']  = 0;
                $_POST['sort']  = 0;

                if(!empty($member_renum)){

                    $where['member_mynum'] = $_POST['member_renum'];
                    $checkre = $member->where($where)->find();
                    if(!$checkre){
                        echo json_encode(array('reTips'=>'Error','reMsg'=>'对不起，推荐码错误请重新填写。'));
                        exit;
                    }

                }

                $checkResult = $member->create();

                if($checkResult){

                    $id = $member->add();

                    $land = M('Land');

                    $landData['land_member_id'] = $id;
                    $landData['land_number'] = $numbered.$id;
                    $landData['land_status'] = 0;
                    $landData['land_type'] = 1;
                    $landData['land_add_time'] = time();

                    $land->add($landData);

                    echo json_encode(array('reTips'=>'Success','reMsg'=>'恭喜您，注册成功。'));
                    exit;

                }else{
                    echo json_encode(array('reTips'=>'Error','reMsg'=>$member->getError()));
                    exit;
                }

            }else{

                echo json_encode(array('reTips'=>'Error','reMsg'=>'对不起，您的验证码错误，请重新填写。'));
                exit;
            }


        }else{
            $this->display();
        }

    }

    //生成验证码
    public function verCode(){
        $config =    array(
            'fontSize'    =>    35,    // 验证码字体大小
            'length'      =>    2,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
            'fontttf '    =>    '5.ttf',
        );
        $Verify =     new \Think\Verify($config);
        $Verify->entry();
    }

    //验证验证码
    public function checkVerCode($code, $id = ''){

        $Verify =     new \Think\Verify();
        return $Verify->check($code,$id);

    }

    //会员登录
    public function memberLogin(){

        if(IS_AJAX){

            $verCode = I('verCode');

            $check = $this->checkVerCode($verCode);

            if($check){

                $data['member_tel'] = I('tel');
                $data['member_pwd'] = md5(I('pwd'));

                //0：未勾选 1：已勾选
                $rememberTel = I('rememberTel');
                $rememberPwd = I('rememberPwd');

                $member = M('Member');
                $result = $member->where($data)->find();

                if($result){

                    if($result['member_state'] == '0'){

                        if($rememberTel == '1'){
                            cookie('member_tel',I('tel'),3600 * 24 * 7);
                        }else{
                            cookie('member_tel',null);
                        }

                        if($rememberPwd == '1'){
                            cookie('member_pwd',I('pwd'),3600 * 24 * 7);
                        }else{
                            cookie('member_pwd',null);
                        }

                        $login['logins'] = $result['logins'] + 1;

                        $member->where(array('member_id'=>$result['member_id']))->save($login);

                        $loginLog = M('MemberLoginLog');

                        $loginData['login_uid'] = $result['member_id'];
                        $loginData['login_time'] = time();
                        $loginData['login_ip'] = get_client_ip();
                        $loginData['login_tel'] = $result['member_tel'];
                        $loginData['login_nickname'] = $result['member_nickname'];
                        $loginData['add_time'] = time();
                        $loginData['display'] = 0;
                        $loginData['sort'] = 0;

                        $loginLog->add($loginData);

                        session('member_id', $result['member_id']);
                        session('member_nickname', $result['member_nickname']);

                        echo json_encode(array('reTips'=>'Success','reMsg'=>'登录成功。'));
                        exit;

                    }else{

                        echo json_encode(array('reTips'=>'Error','reMsg'=>'对不起，您的账号已被停用。'));
                        exit;

                    }

                }else{
                    echo json_encode(array('reTips'=>'Error','reMsg'=>'用户名或密码错误，请重新填写。'));
                    exit;
                }



            }else{
                echo json_encode(array('reTips'=>'Error','reMsg'=>'对不起，您的验证码错误，请重新填写。'));
                exit;
            }



        }

    }

    //忘记密码
    public function memberForgetPwd(){
        $this->display();
    }

    //会员登出
    public function memberLogout(){
        session('member_id', null);
        session('member_nickname', null);

        echo json_encode(array('reTips'=>'Success'));
        exit;
    }

    //会员中心页面
    public function memberCenter(){

        $member = M('Member');
        $memberData = $member->where(array('member_id'=>$_SESSION['member_id']))->find();

        $this->assign('memberData',$memberData);

        $this->display();
    }

    //安全中心页面
    public function memberSafaCenter(){
        $this->display();
    }

    //修改密码
    public function memberUpdatePwd(){

        if(IS_AJAX){

            $rePwd = I('rePwd');
            $newPwd = I('newPwd');

            $member = M('Member');

            $memberData = $member->where(array('member_id'=>$_SESSION['member_id']))->getField('member_pwd');

            if(md5($rePwd) != $memberData){
                $this->error('对不起，您的原密码错误',U('memberUpdatePwd'),1);
            }

            $data['member_pwd'] = md5($newPwd);
            $data['member_id'] = $_SESSION['member_id'];

            $result = $member->save($data);

            if($result){
                $this->success('修改成功',U('memberUpdatePwd'),1);
            }else{
                $this->success('修改失败，不能修改为原密码相同',U('memberUpdatePwd'),1);
            }




        }else{
            $this->display();
        }


    }
	
	
	//我的导师
	public function memberTeacher()
	{
		
        $member = M('Member');
        $memberData = $member->where(array('member_id'=>$_SESSION['member_id']))->find();
		//导师推荐码
		$teacher_code = $memberData['member_renum'];
		if(empty($teacher_code))
		{
			$teacher = '';
		}else{
			$teacher = $member->where(array('member_renum'=>$teacher_code))->find();
		}
		
		
		$this->assign('memberData',$memberData);
		$this->assign('teacher',$teacher);
		$this->display();
	}
	
	//联系资料
	public function memberContact()
	{

        if(IS_AJAX){

            $telphone = I('telphone');
            $wechat = I('wechat');
			$qq = I('qq');
			
            $member = M('Member');          

            $data['member_telphone'] = $telphone;
            $data['member_wechat'] = $wechat;
			$data['member_qq'] = $qq;

            $result = $member->where(array('member_id'=>$_SESSION['member_id']))->save($data);

            if($result){
                $this->success('修改成功',U('memberContact'),1);
            }else{
                $this->success('修改失败',U('memberContact'),1);
            }


        }else{
			$member = M('Member');
			$memberData = $member->where(array('member_id'=>$_SESSION['member_id']))->find();
			$this->assign('memberData',$memberData);
			$this->display();
        }		

	}
	
	//推广列表
	public function memberList()
	{
		$member = M('Member');		
		$memberData = $member->where(array('member_id'=>$_SESSION['member_id']))->find();
		$lists = $member->where('member_renum',$memberData['member_mynum'])->select();
		
		$this->assign('lists',$lists);
		$this->display();
	}
	
	//推广二维码
	public function memberCode()
	{
		$member = M('Member');		
		$memberData = $member->where(array('member_id'=>$_SESSION['member_id']))->find();
		$mynum = $memberData['member_mynum']; // 我的推广码
		$imgCode = qrcode("/index.php/Member/memberReg/id/".$mynum,"{$mynum}");
		
		$this->assign('memberData',$memberData);
		$this->assign('imgCode',$imgCode);
		$this->display();
	}
	
    //果实赠送
	public function cropsGive()
	{
		$this->display();
	}
	
	//果实赠送记录
	public function cropsGiveList()
	{
		$member = M('Member');		
		$memberData = $member->where(array('member_id'=>$_SESSION['member_id']))->find();
		$lists = M('crops_give_log')->where('give_sender_id',$_SESSION['member_id'])->select();
		
		$this->assign('lists',$lists);
		$this->assign('memberData',$memberData);
		$this->display();		
	}
	
	//果实重生
	public function cropsReborn()
	{
		$this->display();
	}
	
	//果实重生记录
	public function cropsRebornList()
	{
		$member = M('Member');		
		$memberData = $member->where(array('member_id'=>$_SESSION['member_id']))->find();
		$lists = M('crops_reborn_log')->where('reborn_member_id',$_SESSION['member_id'])->select();
		
		$this->assign('lists',$lists);
		$this->assign('memberData',$memberData);
		$this->display();	
	}

    //修改绑定手机号
    public function memberUpdatePhone(){

        if(IS_AJAX){

            $rePhone = I('rePhone');
            $newPhone = I('newPhone');

            $member = M('Member');

            $memberData = $member->where(array('member_id'=>$_SESSION['member_id']))->getField('member_tel');

            if($rePhone != $memberData){
                $this->error('对不起，您的原手机号错误',U('memberUpdatePhone'),1);
            }

            $data['member_tel'] = $newPhone;
            $data['member_id'] = $_SESSION['member_id'];

            $result = $member->save($data);

            if($result){
                $this->success('修改成功',U('memberUpdatePhone'),1);
            }else{
                $this->success('修改失败，不能修改为原手机号',U('memberUpdatePhone'),1);
            }


        }else{
            $this->display();
        }


    }
	
	//实名认证
	public function memberCertificated()
	{
		$this->display();
	}
	
}
